﻿namespace SuperFind.Lib.Core
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.ComponentModel;
    using SuperFind.Lib.Data;
    using System.IO;
    using System.Data.Common;
    using System.Data;

    public class FileCache
    {        
        static BackgroundWorker worker = null;
        public FileCache()
        {
            if (worker == null)
            {
                worker = new BackgroundWorker();
                worker.DoWork += new DoWorkEventHandler(worker_DoWork);
                worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
                worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
                worker.WorkerSupportsCancellation = true;
                worker.WorkerReportsProgress = true;
            }
        }
        void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            
        }
        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            
        }
        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            OleDbUtility.TryExecuteNonQuery("DELETE * FROM FileCache");
            Disk disk = new Disk();
            disk.OnFile += new OnFileHandler(disk_OnFile);
            disk.OnFolder += new OnFolderHandler(disk_OnFolder);
            Settings settings = new Settings();
            List<string> locations=settings.LoadLocations();
            if(locations!=null)
            {
                foreach (string location in locations)
                {
                    disk.GetSubDirectoryInfo(location);
                }
            }
        }
        void disk_OnFolder(DirectoryInfo directoryInfo)
        {
            InsertLocation(directoryInfo);
        }
        void disk_OnFile(FileInfo fileInfo)
        {
            InsertLocation(fileInfo);
        }
        public void Refresh()
        {
            if (worker != null && !worker.IsBusy)
                worker.RunWorkerAsync();
        }
        public void StopCaching()
        {

        }
        public void InsertLocation(FileSystemInfo fileSystemInfo)
        {
            string query;
            if (fileSystemInfo is DirectoryInfo)
            {
                DirectoryInfo directoryInfo = fileSystemInfo as DirectoryInfo;
                query = String.Format("INSERT INTO {0} (FileName,FileType,FilePath) VALUES('{1}',{2},'{3}')", "FileCache",directoryInfo.Name,0,directoryInfo.FullName);
            }
            else
            {
                FileInfo fileInfo = fileSystemInfo as FileInfo;
                query = String.Format("INSERT INTO FileCache (FileName,FileType,Extention,FilePath) VALUES('{1}',{2},'{3}','{4}')", "FileCache", fileInfo.Name, 1, fileInfo.Extension, fileInfo.FullName);
            }
            OleDbUtility.TryExecuteNonQuery(query);
        }
        private string _fileFilter = "";
        public string FileFilter 
        { 
            get
            {
                return _fileFilter;
            }
            set
            {
                if (String.IsNullOrWhiteSpace(value))
                    _fileFilter = "FileName = ''";
                else
                    _fileFilter = String.Format("FileName LIKE '*{0}*'", value).ToString();
            }
        }
        public DataView SearchResult 
        { 
            get
            {
                DataView dataView = null;
                DataTable result = new DataTable();
                result = ResultTable.Copy();
                if (result != null)
                {
                    dataView = result.DefaultView;
                    dataView.RowFilter = FileFilter;
                }
                return dataView;
            }
        }
        private DataTable ResultTable 
        { 
            get
            {
                return OleDbUtility.TryExecuteDataTable("SELECT * FROM FILECACHE");
            }
        }
    }
}
